<template>
  <div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" style="width:60%;margin:0 auto 200px;">
      <el-form  ref="dataForm" :model="temp" label-position="right" label-width="100px" style='width: 300px; margin-left:30px;'>
        <el-form-item
          prop="moduleName"
          label="模块名称:"
          :rules="filter_rules({required: true, max: 50, min: 3, type: 'nameBadCharactor'})"
        >
          <el-input v-model="temp.moduleName" size="mini"></el-input>
        </el-form-item>
        <el-form-item
          label="模块描述:"
          prop="description"
          :rules="filter_rules({required:true,max:200,min:3})">
          <el-input type="textarea" v-model="temp.description" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData" size="mini">确 定</el-button>
        <el-button v-else type="primary" @click="updateData" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script type="text/javascript">
  import {yfuPost} from '@/utils/request.js'
  import {saveModuleRemote, updateModuleRemote} from '@/view/api/moduleManager.js'
  import {yfuSuccessToast} from '@/utils/yfuMessage.js'
  export default {
    data () {
      return {
        activeName: 'first',
        temp: {
        },
        textMap: {
          update: '修改模块',
          create: '新建模块'
        },
        dialogStatus: 'update',
        dialogFormVisible: false
      }
    },
    methods: {
      createModuleDialog () {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.temp = {}
      },
      modifyModuleDialog (data) {
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.temp = data
      },
      createData () {
        let cThis = this
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            yfuPost(process.env.BASE_API, saveModuleRemote.url, this.temp, (data) => {
              yfuSuccessToast('保存成功')
              cThis.dialogFormVisible = false
              cThis.$emit('fmModuleToRefreshTable')
            })
          }
        })
      },
      updateData () {
        let cThis = this
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            console.log(updateModuleRemote)
            yfuPost(process.env.BASE_API, updateModuleRemote.url, this.temp, (data) => {
              yfuSuccessToast('保存成功')
              cThis.dialogFormVisible = false
              cThis.$emit('fmModuleToRefreshTable')
            })
          }
        })
      }
    }
  }
</script>

<style>

</style>
